
package com.crv.ci.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crv.common.domain.view.BizData4Page;
import com.crv.ci.model.DbServer;
import com.crv.ci.service.IDbServerService;
import com.crv.common.utils.IdGenerator;
import com.crv.common.utils.JsonResult;
import com.crv.common.utils.PageParam;
import com.crv.common.utils.Constants;

@Controller
public class DbServerAction extends AbstractAdminController<IDbServerService>{

    @Autowired
    private IDbServerService dbServerService;
    
    @RequestMapping(value="/dbserver/list")
    public String list(DbServer dbServer,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(dbServer); 	
    	BizData4Page<DbServer> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("dbServer", dbServer);
    	return "/module/dbserver/list";
    }    
    
    @RequestMapping(value="/dbserver/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("dbServer", new DbServer());
        return "module/dbserver/edit";
    }    
    
    @RequestMapping(value="/dbserver/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	DbServer dbServer = dbServerService.fetch(id);
    	model.addAttribute("dbServer", dbServer);
        return "module/dbserver/edit";
    }    
    
    @RequestMapping(value="/dbserver/save")
    @ResponseBody
    public String save(HttpServletRequest request,DbServer dbServer){
		boolean isAddNew = StringUtils.isBlank(dbServer.getId())?true:false;
		if(isAddNew)
		{
			dbServer.setId(IdGenerator.createNewId());
			dbServerService.add(dbServer);
		}
		else
		{
			dbServerService.update(dbServer);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/dbserver/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	dbServerService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(DbServer dbServer)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("groupId", dbServer.getGroupId());		
		conditions.put("otherParam", dbServer.getOtherParam());		
		conditions.put("dbName", dbServer.getDbName());		
		conditions.put("dbType", dbServer.getDbType());		
		conditions.put("dbAlias", dbServer.getDbAlias());		
		conditions.put("dbHost", dbServer.getDbHost());		
		conditions.put("dbPort", dbServer.getDbPort());		
		conditions.put("dbUser", dbServer.getDbUser());		
		conditions.put("dbPass", dbServer.getDbPass());		
		conditions.put("dbnote", dbServer.getDbnote());		
		conditions.put("createDate", dbServer.getCreateDate());		
    	return conditions;
    }

    @Override
    protected IDbServerService getMainService() {
        return dbServerService;
    }

    @Override
    protected String getMainObjName() {
        return "dbserver";
    }

    @Override
    protected String getViewTitle() {
        return "dbserver";
    }
}
